##Robustness checks for conative attitudes for main endline and midline results

# lc1
lc1_el_passivecovs <- ols_main(
  outcome = "lc1",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = TRUE,
  cluster_SE = TRUE,
  covariates = "abs_passive_option_lc1",
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE)

lc1_el_pvals_passivecovs <- get_RI_pvals(
  outcome = "lc1",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = TRUE,
  cluster_SE = TRUE,
  covariates = "abs_passive_option_lc1",
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE,
  assignment_data = treatment_assignment,
  extract_function = coef,
  analysis_function = ols_main,
  sims = sims,
  lwr_upr_two = "upr")


lc1_ml_passivecovs <- ols_main(
  outcome = "lc1_ml",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = TRUE,
  cluster_SE = TRUE,
  covariates = "abs_passive_option_lc1_ml",
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE)

lc1_ml_pvals_passivecovs <- get_RI_pvals(
  outcome = "lc1_ml",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = TRUE,
  cluster_SE = TRUE,
  covariates = "abs_passive_option_lc1_ml",
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE,
  assignment_data = treatment_assignment,
  extract_function = coef,
  analysis_function = ols_main,
  sims = sims,
  lwr_upr_two = "upr")


# pta

pta_el_passivecovs <- ols_main(
  outcome = "pta",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = TRUE,
  cluster_SE = TRUE,
  covariates = "abs_passive_option_pta",
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE)

pta_el_pvals_passivecovs <- get_RI_pvals(
  outcome = "pta",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = TRUE,
  cluster_SE = TRUE,
  covariates = "abs_passive_option_pta",
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE,
  assignment_data = treatment_assignment,
  extract_function = coef,
  analysis_function = ols_main,
  sims = sims,
  lwr_upr_two = "upr")


pta_ml_passivecovs <- ols_main(
  outcome = "pta_ml",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = TRUE,
  cluster_SE = TRUE,
  covariates = "abs_passive_option_pta_ml",
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE)

pta_ml_pvals_passivecovs <- get_RI_pvals(
  outcome = "pta_ml",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = TRUE,
  cluster_SE = TRUE,
  covariates = "abs_passive_option_pta_ml",
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE,
  assignment_data = treatment_assignment,
  extract_function = coef,
  analysis_function = ols_main,
  sims = sims,
  lwr_upr_two = "upr")


# bring_up

bring_up_el_passivecovs <- ols_main(
  outcome = "bring_up",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = TRUE,
  cluster_SE = TRUE,
  covariates = "abs_passive_option_bring_up",
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE)

bring_up_el_pvals_passivecovs <- get_RI_pvals(
  outcome = "bring_up",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = TRUE,
  cluster_SE = TRUE,
  covariates = "abs_passive_option_bring_up",
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE,
  assignment_data = treatment_assignment,
  extract_function = coef,
  analysis_function = ols_main,
  sims = sims,
  lwr_upr_two = "upr")


bring_up_ml_passivecovs <- ols_main(
  outcome = "bring_up_ml",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = TRUE,
  cluster_SE = TRUE,
  covariates = "abs_passive_option_bring_up_ml",
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE)

bring_up_ml_pvals_passivecovs <- get_RI_pvals(
  outcome = "bring_up_ml",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = TRUE,
  cluster_SE = TRUE,
  covariates = "abs_passive_option_bring_up_ml",
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE,
  assignment_data = treatment_assignment,
  extract_function = coef,
  analysis_function = ols_main,
  sims = sims,
  lwr_upr_two = "upr")


# assemble


assemble_el_passivecovs <- ols_main(
  outcome = "assemble",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = TRUE,
  cluster_SE = TRUE,
  covariates = "abs_passive_option_assemble",
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE)

assemble_el_pvals_passivecovs <- get_RI_pvals(
  outcome = "assemble",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = TRUE,
  cluster_SE = TRUE,
  covariates = "abs_passive_option_assemble",
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE,
  assignment_data = treatment_assignment,
  extract_function = coef,
  analysis_function = ols_main,
  sims = sims,
  lwr_upr_two = "upr")


assemble_ml_passivecovs <- ols_main(
  outcome = "assemble_ml",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = TRUE,
  cluster_SE = TRUE,
  covariates = "abs_passive_option_assemble_ml",
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE)

assemble_ml_pvals_passivecovs <- get_RI_pvals(
  outcome = "assemble_ml",
  treatment = "absenteeism",
  resample_FE = TRUE,
  block_FE = TRUE,
  audience_size = TRUE,
  cluster_SE = TRUE,
  covariates = "abs_passive_option_assemble_ml",
  the_data = subset(el, respondent_category == "Complier"),
  dosage = FALSE,
  dosage_indicator = FALSE,
  assignment_data = treatment_assignment,
  extract_function = coef,
  analysis_function = ols_main,
  sims = sims,
  lwr_upr_two = "upr")




#print robustness tables conative attitudes --------------------------------------------------
#setup
control_means <- with(
	el, 
	c(
		"Control Mean",
		round(mean(lc1_ml[absenteeism == 0 & respondent_category == "Complier"],na.rm = TRUE), 2),
		round(mean(lc1[absenteeism == 0 & respondent_category == "Complier"],na.rm = TRUE), 2),
		round(mean(bring_up_ml[absenteeism == 0 & respondent_category == "Complier"],na.rm = TRUE), 2),
		round(mean(bring_up[absenteeism == 0 & respondent_category == "Complier"],na.rm = TRUE), 2),
		round(mean(pta_ml[absenteeism == 0 & respondent_category == "Complier"],na.rm = TRUE), 2),
		round(mean(pta[absenteeism == 0 & respondent_category == "Complier"],na.rm = TRUE), 2),
		round(mean(assemble_ml[absenteeism == 0 & respondent_category == "Complier"],na.rm = TRUE), 2),
		round(mean(assemble[absenteeism == 0 & respondent_category == "Complier"],na.rm = TRUE), 2)
	)
)


pval_lines <- c(
	"RI $p$-values",
	round(lc1_ml_pvals_passivecovs$ri_pvals["absenteeism"],3),
	round(lc1_el_pvals_passivecovs$ri_pvals["absenteeism"],3),
	round(bring_up_ml_pvals_passivecovs$ri_pvals["absenteeism"],3),
	round(bring_up_el_pvals_passivecovs$ri_pvals["absenteeism"],3),
	round(pta_ml_pvals_passivecovs$ri_pvals["absenteeism"],3),
	round(pta_el_pvals_passivecovs$ri_pvals["absenteeism"],3),
	round(assemble_ml_pvals_passivecovs$ri_pvals["absenteeism"],3),
	round(assemble_el_pvals_passivecovs$ri_pvals["absenteeism"],3)
)

hypothesis_lines <- c(
	"Hypothesis",
	"upr",
	"upr",
	"upr",
	"upr",
	"upr",
	"upr",
	"upr",
	"upr"
)

#make tables
sink("03_tables/ABS_conative_attitudes_passivecovs.tex")
stargazer(
	... = list(
		lc1_ml_passivecovs$fit,
		lc1_el_passivecovs$fit,
		bring_up_ml_passivecovs$fit,
		bring_up_el_passivecovs$fit,
		pta_ml_passivecovs$fit,
		pta_el_passivecovs$fit,
		assemble_ml_passivecovs$fit,
		assemble_el_passivecovs$fit
	),
	type = "latex",
	p = list(
		lc1_ml_pvals_passivecovs$ri_pvals,
		lc1_el_pvals_passivecovs$ri_pvals,
		bring_up_ml_pvals_passivecovs$ri_pvals,
		bring_up_el_pvals_passivecovs$ri_pvals,
		pta_ml_pvals_passivecovs$ri_pvals,
		pta_el_pvals_passivecovs$ri_pvals,
		assemble_ml_pvals_passivecovs$ri_pvals,
		assemble_el_pvals_passivecovs$ri_pvals
	),
	se = list(
		lc1_ml_passivecovs$fit_summary[,"Std. Error"],
		lc1_el_passivecovs$fit_summary[,"Std. Error"],
		bring_up_ml_passivecovs$fit_summary[,"Std. Error"],
		bring_up_el_passivecovs$fit_summary[,"Std. Error"],
		pta_ml_passivecovs$fit_summary[,"Std. Error"],
		pta_el_passivecovs$fit_summary[,"Std. Error"],
		assemble_ml_passivecovs$fit_summary[,"Std. Error"],
		assemble_el_passivecovs$fit_summary[,"Std. Error"]
	),
	keep = "absenteeism",
	omit.stat = c("rsq","f","ser"),
	column.separate = c(2,2,2,2),
	column.labels = c("Involve LC1 Chair","Tell village","Use PTA","Assemble group"),
	table.layout = "=cd#-t-as=n",
	dep.var.labels = c("Midline","Endline",
										 "Midline","Endline",
										 "Midline","Endline",
										 "Midline","Endline"
	),
	dep.var.labels.include = TRUE,
	no.space = T,
	omit = "block_id",
	add.lines = list(
		control_means,
		pval_lines,
		hypothesis_lines,
		c("Block FE",
			"Yes","Yes","Yes",
			"Yes","Yes","Yes", 
			"Yes", "Yes")),
	notes.label = "",
	float = FALSE
	# style = "qje"
)
sink()
